function graphStru=latexAddGraphsSummary(fid,graphStru,outputPath)
%% function graphStru=latexAddGraphs(fid,graphStru,outputPath)
%
% Given a set of graph handles, in *graphStru.handles*, 
% this function will add them to an exisitng
% Tex file with file identifier *fid*. The graphs can already exist as
% pdfs, in which case their names (without pdf extension) 
% should be provided in *graphStru.names*
% or will be written as pdfs inside the code, in which case a path must be
% provided in *outputPath*. Only if *outputPath* is given will the graphs
% be written as pdfs. 
% An optional caption can be added in *graphStru.captions*. 
% 
%% I. Inputs
%
% 1. *fid*            Existing file identifier 
% 
% 
% 2. *graphStru*     Structure 
% 
%   .handles          [N 1] vector of graph handles
%
%   .names            i) [N 1] cell with names of existing graphs/graphs to
%                           be created
%
%                     ii) single string, such that graphs to be created are
%                           <names><#> 
%
%   .captions         i) [N 1] cell with captions or 
%
%                     ii) single string, such that the caption will read 
%                     <caption> <#> out of <N> 
%
% 3. *outputPath*     If provided, the handles will be written as pdfs
%                     using .names. This should be the same path as the one
%                     corresponding to the file in fid, else the tex will
%                     not compile
%
% Alejandro Justiniano Sep 25 2013 

%% 1. Dimensions 
Ngraphs=length(graphStru.handles); 
flagSave=0; 
if nargin ==3 && isempty(outputPath)==false 
    flagSave=1; 
    cucd=pwd; 
    disp(' '); 
    disp('Graphs will be saved as individual pdfs'); 
end 
%% 2. Check graphStru.names, fill in if a string 

% Convert to cell such that know if I have a [1,1] or 
% [N,1] cell array 
if iscell(graphStru.names)==false
    graphStru.names=cellstr(graphStru.names);
end
if flagSave==0 && length(graphStru.names)~=Ngraphs 
    error('graphStru.names must be the length of handles if they are not automatically written by the code'); 
end 
if flagSave==1 
    if length(graphStru.names)==1 
        graphStru.names=fnumauto(char(graphStru.names),Ngraphs,1); 
    elseif length(graphStru.names)~=Ngraphs 
        error('graphStru.names is either [1,1] or [N,1]') 
    end 
end 
%% 3. Captions         
if nargin < 4 || isempty(graphStru.captions)==true 
    graphStru.captions=fillcell(Ngraphs,1,''); 
else 
    if iscell(graphStru.captions)==false 
        graphStru.captions=cellstr(graphStru.captions); 
    end 
    if length(graphStru.captions)==1 
        tempCaption=cell(Ngraphs,1); 
        for ii=1:Ngraphs; 
            tempCaptionSub=fprintf(' %d of %d \n',ii,Ngraphs);
            tempCaption(ii)={ strcat( char(graphStru.captions),tempCaptionSub )};             
        end 
%         graphStru.captions=tempCaption; 
        clear temp*;          
    elseif length(graphStru.captions)~=Ngraphs 
        error('graphStru.captions is either [1,1] or [N,1]') 
    end     
end     
%% 4. Begin Loop 
for iGraph=1:Ngraphs; 
    %% 4.1 Create PDFs
    if flagSave==1
        cd(outputPath);
        print(graphStru.handles(iGraph),'-dpdf',...
            char(graphStru.names(iGraph)));
    end
    %% 4.2 Write PDFs to document 
%     fprintf(fid,'\\begin{figure}[htbp] \\centering\n');
%     tempFile=strcat('\\includegraphics[scale=0.65]{',...
%         '"',char(graphStru.names(iGraph)),'"','.pdf} \n'); 
    tempFile=strcat('\\includepdf[page=-]{',...
        '"',char(graphStru.names(iGraph)),'"','.pdf} \n'); 
    fprintf(fid,tempFile);
    if ~isempty(graphStru.captions{iGraph})
        tempCaption=strcat('\\caption{',char(graphStru.captions(iGraph)),'} \n');
        fprintf(fid,tempCaption);
    end
%     fprintf(fid,'\\end{figure} \n');
%     fprintf(fid,'\\newpage \n');
    clear temp*; 
end
if flagSave==1
    cd(cucd);
end

end

